package edu.whut.mall.admin.service.impl;

import edu.whut.mall.admin.domain.ProductCategoryParams;
import edu.whut.mall.admin.enums.SysStatusEnum;
import edu.whut.mall.admin.service.IProductCategoryService;
import edu.whut.mall.admin.vo.ProductCategoryVo;
import edu.whut.mall.mapper.ProductCategoryMapper;
import edu.whut.mall.model.ProductCategory;
import edu.whut.mall.model.ProductCategoryExample;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: d9lab-mall
 * @description: 商品分类实现类
 * @author: Wayne
 * @create: 2020-04-30 18:44
 **/
@Service
@RequiredArgsConstructor
public class ProductCategoryServiceImpl implements IProductCategoryService {

    private final ProductCategoryMapper productCategoryMapper;

    @Override
    public Integer insertOneProductCategory(ProductCategoryParams productCategoryParams) {
        ProductCategory productCategory = new ProductCategory();
        BeanUtils.copyProperties(productCategoryParams, productCategory);
        productCategory.setStatus(SysStatusEnum.AVAILABLE.getCode());
        return productCategoryMapper.insertSelective(productCategory);
    }

    @Override
    public Integer updateOneProductCategory(ProductCategoryParams productCategoryParams) {
        ProductCategory productCategory = new ProductCategory();
        BeanUtils.copyProperties(productCategory, productCategoryParams);
        productCategory.setUpdateTime(new Date());
        return productCategoryMapper.updateByPrimaryKeySelective(productCategory);
    }



    @Override
    public List<ProductCategoryVo> listProductCategoryVos() {
        List<ProductCategory> productCategoryList = productCategoryMapper.selectByExample(new ProductCategoryExample());
        return transform2TreeProductCategoryVo(productCategoryList);
    }

    /**
     * 将分类树形化展示
     * @param productCategoryList
     * @return
     */
    private List<ProductCategoryVo> transform2TreeProductCategoryVo(List<ProductCategory> productCategoryList){
           List<ProductCategoryVo> productCategoryVoList=productCategoryList.stream().filter(productCategory ->productCategory.getParentId().equals(0L))
                   .map(productCategory -> covert(productCategory,productCategoryList))
                   .collect(Collectors.toList());
           return productCategoryVoList;
    }


    /**
     * 从数据库转换成展示层
     * @param productCategory
     * @return
     */
    private ProductCategoryVo trans2Vo(ProductCategory productCategory){
        ProductCategoryVo productCategoryVo = new ProductCategoryVo();
        BeanUtils.copyProperties(productCategory,productCategoryVo);
        return productCategoryVo;
    }

    private ProductCategoryVo covert(ProductCategory parentNode, List<ProductCategory> productCategoryList){
        ProductCategoryVo parentNodeVo = trans2Vo(parentNode);
        List<ProductCategoryVo> subProductCategoryVos=productCategoryList.stream().filter(subNode->subNode.getParentId().equals(parentNodeVo.getId()))
                .map(subNode->covert(subNode,productCategoryList))
                .sorted(Comparator.comparing(ProductCategory::getSort))
                .collect(Collectors.toList());
        parentNodeVo.setSubProductCategoryVos(subProductCategoryVos);
        return parentNodeVo;
    }
}

